
package com.joolun.cloud.common.nacos.util;

import com.alibaba.cloud.nacos.NacosConfigProperties;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;

/**
 * nacos配置文件操作工具类
 */
@Slf4j
@Configuration
@AllArgsConstructor
public class NacosConfigUtils {

	private NacosConfigProperties nacosProperties;

	/**
	 * 获取ConfigService
	 * @return
	 * @throws NacosException
	 */
	public ConfigService getConfigService() throws NacosException {
		Properties properties = new Properties();
		properties.put(PropertyKeyConst.SERVER_ADDR, nacosProperties.getServerAddr());
		properties.put(PropertyKeyConst.USERNAME, nacosProperties.getUsername());
		properties.put(PropertyKeyConst.PASSWORD, nacosProperties.getPassword());
		ConfigService configService = NacosFactory.createConfigService(properties);
		return configService;
	}
}
